install.packages("mediation"); install.packages("foreign")

library(mediation);library(foreign)


dd<-read.dta(file="data_fig_a7.dta")

################################################


m1<-lm(iron~lu12,data=dd)

y1<-lm(Entrants~iron+lu12,data=dd)

med1<-mediate(m1,y1,treat="lu12",mediator="iron",data=dd,sims=5000)



m1a<-glm(iron~lu12,data=dd,family="poisson")

y1a<-glm(Entrants~iron+lu12,data=dd,family="poisson")

med1a<-mediate(m1a,y1a,treat="lu12",mediator="iron",data=dd,sims=5000)







#################


m2<-lm(textile~lu12,data=dd)

y2<-lm(Entrants~textile+lu12,data=dd)

med2<-mediate(m2,y2,treat="lu12",mediator="textile",data=dd,sims=5000)

m2a<-lm(textile~lu12+X+Y+rivers,data=dd)

y2a<-lm(Entrants~textile+lu12+X+Y+rivers,data=dd)

med2a<-mediate(m2a,y2a,treat="lu12",mediator="textile",data=dd,sims=5000)





#####################


m3<-lm(total~lu12,data=dd)

y3<-lm(Entrants~total+lu12,data=dd)

med3<-mediate(m3,y3,treat="lu12",mediator="total",data=dd,sims=5000)


m3a<-lm(total~lu12+X+Y+rivers,data=dd)

y3a<-lm(Entrants~total+lu12+X+Y+rivers,data=dd)

med3a<-mediate(m3,y3,treat="lu12",mediator="total",data=dd,sims=5000)



meds<-list(med1,med2,med3)

acme<-rep(NA,3)

ade<-rep(NA,3)

prop<-rep(NA,3)


lome<-rep(NA,3)

upme<-rep(NA,3)


lod<-rep(NA,3)

upd<-rep(NA,3)

loprop<-rep(NA,3)

upprop<-rep(NA,3)


med_eff<-matrix(NA,6,3)




for(i in 1:3){
	
acme[i]<-summary(meds[[i]])[1]

ade[i]<-summary(meds[[i]])[9]	

prop[i]<-summary(meds[[i]])[17]

upme[i]<-summary(meds[[i]])[[3]][2]	

lome[i]<-summary(meds[[i]])[[3]][1]	


upd[i]<-summary(meds[[i]])[[11]][2]	

lod[i]<-summary(meds[[i]])[[11]][1]	


loprop[i]<-summary(meds[[i]])[[19]][1]	

upprop[i]<-summary(meds[[i]])[[19]][2]	

}



med_eff[1,]<-round(do.call(cbind,acme),2)

med_eff[3,]<-round(do.call(cbind,ade),2)

med_eff[2,]<-paste("[",round(lome,2),round(upme,2),"]",sep=",")

med_eff[4,]<-paste("[",round(lod,2),round(upd,2),"]",sep=",")

med_eff[5,]<-round(do.call(cbind,prop),2)

med_eff[6,]<-paste("[",round(loprop,2),round(upprop,2),"]",sep=",")

########################

med_eff2<-matrix(NA,6,6)

med_eff2[,1]<-med_eff[,1]

med_eff2[,2]<-"&"

med_eff2[,3]<-med_eff[,2]

med_eff2[,4]<-"&"

med_eff2[,5]<-med_eff[,3]

med_eff2[,6]<-"\\"



###### First Three Columns ######

med_effs1<-data.frame(med_eff2)


print("The First Three Columns")

print(med_effs1)

##########################

##########################

##########################




####################



m1a<-lm(iron~lu12,data=dd)

y1a<-glm(Entrants~iron+lu12,data=dd,family="poisson")

med1a<-mediate(m1a,y1a,treat="lu12",mediator="iron",data=dd,sims=5000)





m2a<-lm(textile~lu12,data=dd)

y2a<-glm(Entrants~textile+lu12,data=dd,family="poisson")

med2a<-mediate(m2a,y2a,treat="lu12",mediator="textile",data=dd,sims=5000)





#####################



m3a<-lm(total~lu12,data=dd)

y3a<-glm(Entrants~total+lu12,data=dd,family="poisson")


med3a<-mediate(m3,y3,treat="lu12",mediator="total",data=dd,sims=5000)



meds<-list(med1a,med2a,med3a)

acme<-rep(NA,3)

ade<-rep(NA,3)

prop<-rep(NA,3)


lome<-rep(NA,3)

upme<-rep(NA,3)


lod<-rep(NA,3)

upd<-rep(NA,3)

loprop<-rep(NA,3)

upprop<-rep(NA,3)


med_eff<-matrix(NA,6,3)




for(i in 1:3){
	
acme[i]<-summary(meds[[i]])[1]

ade[i]<-summary(meds[[i]])[9]	

prop[i]<-summary(meds[[i]])[17]

upme[i]<-summary(meds[[i]])[[3]][2]	

lome[i]<-summary(meds[[i]])[[3]][1]	


upd[i]<-summary(meds[[i]])[[11]][2]	

lod[i]<-summary(meds[[i]])[[11]][1]	


loprop[i]<-summary(meds[[i]])[[19]][1]	

upprop[i]<-summary(meds[[i]])[[19]][2]	

}



med_eff[1,]<-round(do.call(cbind,acme),2)

med_eff[3,]<-round(do.call(cbind,ade),2)

med_eff[2,]<-paste("[",round(lome,2),round(upme,2),"]",sep=",")

med_eff[4,]<-paste("[",round(lod,2),round(upd,2),"]",sep=",")

med_eff[5,]<-round(do.call(cbind,prop),2)

med_eff[6,]<-paste("[",round(loprop,2),round(upprop,2),"]",sep=",")

########################

med_eff2<-matrix(NA,6,6)

med_eff2[,1]<-med_eff[,1]

med_eff2[,2]<-"&"

med_eff2[,3]<-med_eff[,2]

med_eff2[,4]<-"&"

med_eff2[,5]<-med_eff[,3]

med_eff2[,6]<-"\\"


##### Last Three Columns ######


print("The Last Three Columns")

med_effs2<-data.frame(med_eff2)

print(med_effs2)


